package cn.test.Async.demo;

import java.util.concurrent.CountDownLatch;
import static cn.test.Async.AsyncCall.threadName;

/**
 * create by zsunny
 * data: 2018/8/5
 **/
//利用countdownlatch  原理同Demo1
public class Demo4 extends BaseDemo{

    private final CountDownLatch countDownLatch = new CountDownLatch(1);

    @Override
    public void callback(long response) {

        System.out.println(threadName() +"得到结果");
        System.out.println(threadName() +response);
        System.out.println(threadName() +"调用结束");

        countDownLatch.countDown();//自减1，等到countDownLatch ==0时，唤醒主线程
    }

    public static void main(String[] args) {
        Demo4 demo4 = new Demo4();
        demo4.call();
        try {
            demo4.countDownLatch.await();//countDownLatch ==0时，唤醒
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("主线程内容");

    }
}
